//
//  TWSquareCardCollectionView.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/30.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit

class TWSquareCardCollectionView: UICollectionView {

    // MARK:- 属性
    var videosArray : [squareCardModel]?{
        didSet {
            guard let videosArray = videosArray else {
                return
            }
            dataArray = videosArray
        }
    }
    var dataArray = [squareCardModel]()
    var flowLayout = UICollectionViewFlowLayout()
    
    // MARK:- 系统回调
    override func awakeFromNib() {
        super.awakeFromNib()
        initCollectionView()
    }
    
    private func initCollectionView(){
        // 初始化布局
        flowLayout.minimumInteritemSpacing = 2
        flowLayout.scrollDirection = .horizontal
        flowLayout.itemSize = CGSize(width: squareCardCollectionViewHeight, height: squareCardCollectionViewHeight)
        // 初始化CollectionView
        collectionViewLayout = flowLayout
        showsHorizontalScrollIndicator = false
        delegate = self
        dataSource = self
        register(UINib(nibName: "TWSquareCardCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: SquareCardCollectionViewCell)
    }
}


extension TWSquareCardCollectionView : UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SquareCardCollectionViewCell, for: indexPath) as! TWSquareCardCollectionViewCell
        cell.model = dataArray[indexPath.item]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = dataArray[indexPath.item]
        TWLog(message: model.title)
    }
}

